package org.linlinjava.litemall.db.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class CommonUtils {

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new HashMap<>(16);
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    public static Integer plus(Integer a, Integer b) {
        return a + b;
    }

    public static BigDecimal plus(BigDecimal... numbers) {
        Stream<BigDecimal> bigDecimalStream = Stream.of(numbers);
        return bigDecimalStream.reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}
